var SPSdk = (function () {
    var GAME_URL = "http://game.center.1kxun.mobi/js/game.js";

    var self = null;

    function SPSdk() {
        this.access_token = "";//SP的会话令牌 token
        this.appId = "";//SP给游戏分配的appid
        this.spId = "";//layabox给SP分配的id
        this.appKey = "";
        this.userId = "";//SP给玩家分配的id

        self = this;
    }

    var __proto = SPSdk.prototype;

    __proto.init = function (param, callback) {
        self.access_token = param.queryParams.access_token;
        self.appKey = param.appKey;
        LayaCommon.loadScript(GAME_URL, function () {
            callback({"result": 0, "desc": "OK"});
        }, function () {
            callback({"result": -1, "desc": "plugin download error"});
        });
    };

    __proto.login = function (param, callback) {
        if (popupLoginView) {
            console.log("function popupLoginView found");
            window.addEventListener("message", function (param) {
                console.log(JSON.stringify(param));
                callback && callback({
                    "result": 0,
                    "desc": "OK",
                    "spuid": self.userId + "",
                    "sessionKey": self.access_token
                });
            });
            popupLoginView(self.access_token, "qxLoginCallback");

        }
        else {
            console.log("function popupLoginView not found");
        }
    };

    __proto.pay = function (param, callback) {
        var m_this = this;
        var payObject = {
            app_key: self.appKey,
            access_token: self.access_token,
            cost: param.data.amount,
            extra_data: param.data.orderId,
            product_name: param.data.goodsName,
            game_name: ""
        };
        popupPayView(payObject, function (param1, param2) {
            if (param1)
                callback && callback({"resul;t": 0, "desc": "OK", "data": param2});
            else
                callback && callback({"resul;t": -1, "desc": "pay failed", "data": param2});
        });
    };

    return SPSdk;
})();
